package com.collabera.labs.sai;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class InvokingActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        Button invokingButton = (Button)findViewById(R.id.invokebutton);
        invokingButton.setOnClickListener(new OnClickListener() {
        	
        	public void onClick(View v) {
        		Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
        		startActivity(explicitIntent);
        	}
        });
    }
    
    public static String login() {

        HttpClient client = new DefaultHttpClient();


        HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); // Timeout
        // Limit
        HttpResponse response;
        JSONObject json = new JSONObject();
        try {

        	String url = "http://localhost/testando/yourPageHere.php";
            HttpPost post = new HttpPost(url);

            post.setHeader("Accept", "application/json");
            post.setHeader("Content-type", "application/json");

            json.put("email", "raissaa@gmail.com");
            json.put("password", "blahhh");

            StringEntity se = new StringEntity("{\"User\":" + json.toString()
                    + "}");
            post.setEntity(se);
            response = client.execute(post);
            if (response != null)
            	System.out.println(response.getAllHeaders());
                return response.toString();
            //return new String(response.getString("token"));
            /**if (response != null) {
                json = getResult(response);

                if(statusOK(json)){
                    return new String(json.getString("token")); 
                }
            }*/
        } catch (Exception e) {
            Log.e("Error", "Error Login", e);
        }
        //  printJSON(json);

        return "ERROR";
    }
    
    /**
    public void teste() throws ClientProtocolException, IOException, JSONException {
    	HttpClient httpclient = new DefaultHttpClient();
        String url = "http://localhost/testando/yourPageHere.php";
        HttpPost httppost = new HttpPost(url);
        JSONObject json = new JSONObject();

        json.put("username", "bob");
        json.put("email", "test@testsite.com");
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();

        nvps.add(new BasicNameValuePair("value", json.toString()));
        httppost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));                 

        URL test_url = new URL(url);
        URLConnection connection = test_url.openConnection();
        connection.setDoOutput(true);

        HttpResponse response;
        response = httpclient.execute(httppost);
        Log.i("NVPS",nvps.get(0).toString());
        Log.i("JSON",json.toString());
        Log.i("response", response.getEntity().getContent().toString());
        Log.i("response status",response.getStatusLine().toString());

        BufferedReader in = new BufferedReader(
              new InputStreamReader(
              connection.getInputStream()));

        String decodedString;

        while ((decodedString = in.readLine()) != null) {
           //System.out.println(decodedString);
           Log.i("info 10",decodedString);
        }
        in.close();
    }
    */
    
    /**
    public void testeDoSite() {
    	HttpClient httpClient = new DefaultHttpClient();
    	HttpPost httpPost = new HttpPost("http://localhost/yourPageHere.php");
    	List<BasicNameValuePair> nameValuePairs = new ArrayList<BasicNameValuePair>();
    	nameValuePairs.add(new BasicNameValuePair("email", "blah@blah.com"));
    	nameValuePairs.add(new BasicNameValuePair("password", "oiola"));
    	nameValuePairs.add(new BasicNameValuePair("data", "algumacoisa"));

    	try {
    	    httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    	} catch (UnsupportedEncodingException e) {
    	    //do stuff
    	}

    	HttpResponse response = null;
    	try {
    	    response = httpClient.execute(httpPost);
    	} catch (ClientProtocolException e) {
    	    //do stuff
    	} catch (IOException e) {
    	    //do stuff
    	}

    	if(response == null){
    	    //time out or other problem, fail gracefully

    	}

    	HttpEntity responseEntity = response.getEntity();

    	try {
    	    String jsonString = EntityUtils.toString(responseEntity);
    	    //jsonString is the full body of the response from the server
    	    //if your php script is sending json, thats whats in here
    	} catch (ParseException e) {
    	    //do stuff
    	} catch (IOException e) {
    	    //do stuff
    	}
    }
    */
}